﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Services;
using System.Web.UI.WebControls;
using Meqs.Entity;
using Meqs.Entity.Parameters;
using Meqs.Logic;
using Meqs.Logic.Interface;
using Meqs.Utilities;

public partial class Moral_Teacher_ScoreInput_ScoreInput : ScoreInputBasePage
{
    #region 参数列表

    protected string ReportTitle
    {
        get
        {
            string title = "{0}{1} 德育{2}报表";

            int schoolYear = SchoolYear;
            int term = Term;
            if (SchoolYear == -1 || Term == -1)
            {
                ISchoolInfoLogic schLogic = Container<ISchoolInfoLogic>.Instance;
                SchoolInfo sch = schLogic.FindBySchoolMark(SchoolMark);
                schoolYear = sch.SchoolYear;
                term = sch.SchoolTerm;
            }
            string termStr = term == 1 ? "上学期" : "下学期";
            return string.Format(title, Common.ConvertSchoolYear(schoolYear), termStr, Common.ReportDateType(3));
        }
    }

    protected static string ExcelFileName
    {
        get
        {
            string title = "{0}{1} 德育{2}报表";

            int schoolYear = SchoolYear;
            int term = Term;
            if (SchoolYear == -1 || Term == -1)
            {
                ISchoolInfoLogic schLogic = Container<ISchoolInfoLogic>.Instance;
                SchoolInfo sch = schLogic.FindBySchoolMark(SchoolMark);
                schoolYear = sch.SchoolYear;
                term = sch.SchoolTerm;
            }
            string termStr = term == 1 ? "上学期" : "下学期";
            string temp = string.Format(title, Common.ConvertSchoolYear(schoolYear), termStr, Common.ReportDateType(3));
            return temp.Replace(" ", "");
            return temp;
        }
    }

    protected string ReportSubTitle
    {
        get
        {
            string title = "班级：{0} &nbsp;&nbsp;  {1}年 {2}月";
            IClsInfoLogic clsLogic = Container<IClsInfoLogic>.Instance;
            ClsInfo cls = clsLogic.FindById(ClsID);
            return string.Format(title, cls.ClsName, Year,Month);
        }
    }
    protected static int weekIndex
    {
        get
        {
            int weekIndex;
            if (int.TryParse(HttpContext.Current.Request["weekIndex"], out weekIndex)) return weekIndex;
            return 0;
        }
    }
    protected static int Day
    {
        get
        {
            //int day;
            //if (int.TryParse(HttpContext.Current.Request["Day"], out day)) return day;
            //return 0;
            return 1;
        }
    }
    private int _tableWidth;

    #endregion

    #region 绑定表格

    protected void BindHeadTable()
    {
        _tableWidth = 0;
        IMoralityTypeLogic mtLogic = Container<IMoralityTypeLogic>.Instance;
        IMoralityItemLogic miLogic = Container<IMoralityItemLogic>.Instance;
        IList<MoralityType> list;
        if (SchoolYear == -1 || Term == -1)
        {
            list = mtLogic.GetAllMoralityType(SchoolMark);
        }
        else
        {
            list = mtLogic.GetAllMoralityType(SchoolMark, SchoolYear, Term);
        }

        TableRow row = new TableRow();
        TableRow subRow = new TableRow();
        row.Style["background-color"] = "#D4F1F9";
        subRow.Style["background-color"] = "#D4F1F9";

        TableCell cell1 = new TableCell();
        cell1.Text = "序号";
        cell1.RowSpan = 2;
        cell1.CssClass = "FixedRowCol";
        cell1.Style["width"] = "40px";
        row.Cells.Add(cell1);
        _tableWidth += 40;

        TableCell cell2 = new TableCell();
        cell2.Text = "姓名";
        cell2.RowSpan = 2;
        cell2.CssClass = "FixedRowCol";
        cell2.Style["width"] = "100px";
        row.Cells.Add(cell2);
        _tableWidth += 100;

        int index = 0;
        foreach (MoralityType mt in list)
        {
            TableCell cell = new TableCell();
            cell.Text = mt.Caption;
            IList<MoralityItem> miList = miLogic.GetItemByType(SchoolMark, mt.RID);
            cell.ColumnSpan = miList.Count;
            cell.CssClass = "FixedRow";
            cell.Style["width"] = (60 * miList.Count) + "px";
            _tableWidth += 60 * miList.Count;
            foreach (MoralityItem item in miList)
            {
                TableCell subCell = new TableCell();
                subCell.Text = item.Caption + (item.Type == 1 ? String.Format("({0}分)", item.Param * item.Operator) : "(次)");
                subCell.Style["width"] = "60px";
                subCell.CssClass = "FixedRow";
                subCell.Attributes.Add("rName", item.Caption);
                subCell.Attributes.Add("index", index.ToString());
                subRow.Cells.Add(subCell);
                ParamVal pv = new ParamVal();
                pv.Index = index;
                pv.ItemID = item.RID;
                ParamVals.Add(pv);
                index++;
            }
            row.Cells.Add(cell);
        }
        TableCell cell3 = new TableCell();
        cell3.Text = "备注";
        cell3.RowSpan = 2;
        cell3.CssClass = "FixedRow";
        cell3.Style["width"] = "200px";
        row.Cells.Add(cell3);
        _tableWidth += 200;
        tblHead.Rows.Add(row);
        tblHead.Rows.Add(subRow);
        tblHead.Style["width"] = _tableWidth + "px";
    }

    protected void BindBodyTable()
    {
        IStudentInfoLogic sLogic = Container<IStudentInfoLogic>.Instance;
        IMoralityDetailLogic mdLogic = Container<IMoralityDetailLogic>.Instance;
        IMoralityRemarkLogic mrLogic = Container<IMoralityRemarkLogic>.Instance;
        IList<StudentInfo> stuList = null;
        if (SchoolCommon.Sch_SchoolInfo.SchoolYear == SchoolYear)
            stuList = sLogic.GetStudentByCls(SchoolMark, ClsID);
        else
            stuList = Container<IStudentHistoryLogic>.Instance.FindHistoryStudents(SchoolYear, Term, ClsID, SchoolMark);
        if (stuList == null)
            return;
        int i = 1;
        int j = 0;
        foreach (StudentInfo info in stuList)
        {
            TableRow row = new TableRow();
            TableCell cellSNo = new TableCell();
            if (j == 0) cellSNo.Style["border-top"] = "0px";
            cellSNo.Text = i + "&nbsp;";
            cellSNo.CssClass = "FixedCol";
            cellSNo.Style["width"] = "40px";
            cellSNo.Style["text-align"] = "right";
            row.Cells.Add(cellSNo);
            i++;

            TableCell cellSName = new TableCell();
            if (j == 0) cellSName.Style["border-top"] = "0px";
            cellSName.Text = "&nbsp;" + info.StudentName;
            cellSName.CssClass = "FixedCol";
            cellSName.Style["width"] = "100px";
            cellSName.Style["text-align"] = "left";
            row.Cells.Add(cellSName);

            IList<ScoreInputEntity> sieList = mdLogic.GetScoreInputEntityByStudent(info.RID, ClsID, 2, Year, Month, Day, SchoolYear, Term, SchoolMark);
            IList<ParamVal> pvList = new List<ParamVal>();
            foreach (ScoreInputEntity item in sieList)
            {
                ParamVal pv = new ParamVal();
                pv.ItemID = item.ItemID;
                if (!ParamVals.Contains(pv)) continue;
                ParamVal p = ParamVals[ParamVals.IndexOf(pv)];
                p.Type = item.Type;
                p.Param = item.Param;
                p.Score = item.Score;
                p.Operator = item.Operator;
                pvList.Add(p);
            }
            ListSort<ParamVal> sort = new ListSort<ParamVal>(pvList, "Index", true);
            pvList = sort.Sort();
            foreach (ParamVal pv in pvList)
            {
                TableCell subCell = new TableCell();
                if (j == 0) subCell.Style["border-top"] = "0px";

                subCell.Style["width"] = "60px";
                // 计次
                if (pv.Type == 2)
                {
                    // 可编辑状态
                    if (IsModify)
                    {
                        string html = string.Format(ScoreInputNumericHtmlStr, "txt_" + pv.ItemID + "_" + info.RID + "_2", pv.Param == 0 ? "" : pv.Param.ToString(), pv.Score, pv.Operator);
                        subCell.Text = html;
                        subCell.Attributes.Add("onmouseover", "try{mover(this);}catch(e){}");
                        subCell.Attributes.Add("onmouseout", "try{mout(this);}catch(e){}");
                    }
                    else
                    {
                        subCell.Style["text-align"] = "right";
                        subCell.Text = pv.Param == 0 ? "" : pv.Param.ToString();
                    }
                }
                // 计分
                if (pv.Type == 1)
                {
                    // 可编辑状态
                    if (IsModify)
                    {
                        string html = string.Format(ScoreInputScoreHtmlStr, "txt_" + pv.ItemID + "_" + info.RID + "_1", pv.Param == 0 ? "" : pv.Param.ToString(), pv.Score);
                        subCell.Text = html;
                    }
                    else
                    {
                        subCell.Style["text-align"] = "right";
                        subCell.Text = pv.Param == 0 ? "" : pv.Param.ToString();
                    }
                }

                row.Cells.Add(subCell);
            }

            // 备注
            MoralityRemark mr = mrLogic.GetRemarkByStudent(info.RID, 3, Year, Month, SchoolYear, Term, SchoolMark);
            TableCell cellRemark = new TableCell();
            if (j == 0) cellRemark.Style["border-top"] = "0px";
            string span = "&nbsp;<span id=\"span_{0}\" title=\"{1}\">{2}</span>";
            span = string.Format(span, info.RID, mr == null ? "" : mr.Data, mr == null ? "" : mr.Data.Length > 20 ? mr.Data.Substring(0, 20) + "..." : mr.Data);
            cellRemark.Text = span;
            cellRemark.Attributes.Add("StudentID", info.RID.ToString());
            cellRemark.Attributes.Add("StudentName", info.StudentName);
            if (IsModify) cellRemark.Attributes.Add("onclick", "displayRemark(this)");
            cellRemark.Style["width"] = "200px";
            cellRemark.Style["text-align"] = "left";
            row.Cells.Add(cellRemark);
            tblHead.Rows.Add(row);
            tblHead.Style["width"] = _tableWidth + "px";
        }

    }

    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindHeadTable();
            BindBodyTable();

            ClientScript.RegisterStartupScript(GetType(), "script", "setTableHeight();", true);
        }
    }

    /// <summary>
    /// 保存单据
    /// </summary>
    /// <param name="jsonStr"></param>
    /// <param name="clsID"></param>
    /// <param name="schoolYear"></param>
    /// <param name="term"></param>
    /// <param name="year"></param>
    /// <param name="month"></param>
    /// <returns></returns>
    [WebMethod]
    public static bool SaveScoreInput(string jsonStr, int clsID, int schoolYear, int term, int year, int month,int day)
    {
        try
        {
            IList<Items> list = ConvertJson(jsonStr);
            IMoralitySubmitRecordLogic msrLogic = Container<IMoralitySubmitRecordLogic>.Instance;
            int? syear = schoolYear == -1 ? (int?)null : schoolYear;
            int? sterm = term == -1 ? (int?)null : term;
            return msrLogic.SaveOrUpdateRecord(list, clsID, 2, year, month,1, syear, sterm, Common.GetUserInfo().UID, SchoolMark);
        }
        catch (Exception)
        {

            throw;
        }
    }

    private static IList<Items> ConvertJson(string jsonStr)
    {
        JavaScriptSerializer jss = new JavaScriptSerializer();
        return jss.Deserialize<IList<Items>>(jsonStr);
    }
    protected void btnReflesh_Click(object sender, EventArgs e)
    {
        tblHead.Rows.Clear();
        tblHead.Rows.Clear();
        BindHeadTable(); 
        BindBodyTable();
    }

    [WebMethod]
    public static string GetExcel(ExcelTableCell[][] cells)
    {
        string temp_path = HttpContext.Current.Server.MapPath("../../../");
        string save_path = temp_path + "\\download\\" + ExcelFileName + ".xls";
        HttpContext.Current.Session["filepath"] = save_path;
        ExcelUtil.ExcelExport(save_path, "德育报表", 2, 0, cells);
        return "";
    }
}
